package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.entity.Comment;
import com.example.entity.CommentLike;
import com.example.entity.vo.CommentVO;
import com.example.service.CommentService;
import com.example.util.BeanFactroy;
import com.example.util.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description: 评论控制器
 * @Date: 2022/11/15 16:03
 * @Author: SinkDream
 */
@WebServlet(name = "CommentController",value = "/comment/*")
public class CommentController extends HttpServlet {
    private CommentService commentService = BeanFactroy.createBean(CommentService.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        String path = req.getPathInfo();

        if ("/listComment".equals(path)){
            listComment(req, resp);
        }
        if ("/listCommentLike".equals(path)){
            listCommentLike(req, resp);
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        String path = req.getPathInfo();

        if ("/addComment".equals(path)){
            addComment(req, resp);
        }
        if ("/deleteComment".equals(path)){
            deleteComment(req,resp);
        }
        if ("/addCommentLike".equals(path)){
            addCommentLike(req,resp);
        }


    }

    //查询评论
    private void listComment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long articleId = Long.parseLong(req.getParameter("articleId"));
        List<CommentVO> comments = commentService.listCommentByArticleId(articleId);
        int count = commentService.countComment(articleId);
        resp.getWriter().print(JSON.toJSONString(R.ok().data("comments",comments).data("commentCount",count)));
    }

    //增加评论
    private void addComment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long userId = Long.parseLong(req.getParameter("userId"));
        Long articleId = Long.parseLong(req.getParameter("articleId"));
        String content = req.getParameter("content");
        String parentIdStr = req.getParameter("parentId");

        Long parentId = new Long("-1");
        Comment comment = new Comment();
        if (parentIdStr != null){
            parentId = Long.parseLong(parentIdStr);
        }
        comment.setParentCommentId(parentId);
        comment.setContent(content);
        comment.setUserId(userId);
        comment.setArticleId(articleId);
        int res = commentService.addComment(comment);
        if (res > 0){
            resp.getWriter().print(JSON.toJSONString(R.ok()));
        }else {
            resp.getWriter().print(JSON.toJSONString(R.error()));
        }
    }

    //删除评论
    private void deleteComment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String[] ids = req.getParameterValues("ids");
        commentService.deleteComment(ids);
        resp.getWriter().print(JSON.toJSONString(R.ok()));
    }

    //评论点赞
    private void addCommentLike(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long userId = Long.parseLong(req.getParameter("userId"));
        Long commentId = Long.parseLong(req.getParameter("commentId"));
        Integer isLike = Integer.parseInt(req.getParameter("isLike"));

        CommentLike commentLike = new CommentLike();
        commentLike.setUserId(userId);
        commentLike.setCommentId(commentId);
        int res = 0;
        if (isLike == 1){
            res = commentService.addCommentLike(commentLike);
            if (res > 0){
            resp.getWriter().print(JSON.toJSONString(R.ok().data("msg","add")));
            }else {
                resp.getWriter().print(JSON.toJSONString(R.error()));
            }

        }else if (isLike == 0){
            res = commentService.removeCommentLike(commentLike);
            if (res > 0){
                resp.getWriter().print(JSON.toJSONString(R.ok().data("msg","remove")));
            }else {
                resp.getWriter().print(JSON.toJSONString(R.error()));
            }

        }

    }

    //查询当前登录的用户是否已经对评论点赞
    private void listCommentLike(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Long userId = Long.parseLong(req.getParameter("userId"));
        List<CommentLike> commentLikes = commentService.listCommentLike(userId);
        resp.getWriter().print(JSON.toJSONString(R.ok().data("commentLikes",commentLikes)));
    }

}
